
if (!RedactorPlugins)
    var RedactorPlugins = {};

RedactorPlugins.fullscreen = {
    init: function()
    {
        this.fullscreen = false;

        this.buttonAdd('fullscreen', 'Fullscreen', $.proxy(this.toggleFullscreen, this));

        if (this.opts.fullscreen)
            this.toggleFullscreen();
    },
    enableFullScreen: function()
    {
        this.buttonChangeIcon('fullscreen', 'normalscreen');
        this.buttonActive('fullscreen');
        this.fullscreen = true;

        if (this.opts.toolbarExternal)
        {
            this.toolcss = {};
            this.boxcss = {};
            this.toolcss.width = this.$toolbar.css('width');
            this.toolcss.top = this.$toolbar.css('top');
            this.toolcss.position = this.$toolbar.css('position');
            this.boxcss.top = this.$box.css('top');
        }

        this.fsheight = this.$editor.height();
        if (this.opts.iframe)
            this.fsheight = this.$frame.height();

        if (this.opts.maxHeight)
            this.$editor.css('max-height', '');
        if (this.opts.iframe)
            var html = this.get();

        if (!this.$fullscreenPlaceholder)
            this.$fullscreenPlaceholder = $('<div/>');
        this.$fullscreenPlaceholder.insertAfter(this.$box);

        this.$box.appendTo(document.body);

        this.$box.addClass('redactor_box_fullscreen');
        $('body, html').css('overflow', 'hidden');

        if (this.opts.iframe)
            this.fullscreenIframe(html);

        this.fullScreenResize();
        $(window).resize($.proxy(this.fullScreenResize, this));
        $(document).scrollTop(0, 0);

        this.focus();
        this.observeStart();
    },
    disableFullScreen: function()
    {
        this.buttonRemoveIcon('fullscreen', 'normalscreen');
        this.buttonInactive('fullscreen');
        this.fullscreen = false;

        $(window).off('resize', $.proxy(this.fullScreenResize, this));
        $('body, html').css('overflow', '');

        this.$box.insertBefore(this.$fullscreenPlaceholder);
        this.$fullscreenPlaceholder.remove();

        this.$box.removeClass('redactor_box_fullscreen').css({width: 'auto', height: 'auto'});

        if (this.opts.iframe)
            html = this.$editor.html();

        if (this.opts.iframe)
            this.fullscreenIframe(html);
        else
            this.sync();

        var height = this.fsheight;
        if (this.opts.autoresize)
            height = 'auto';
        if (this.opts.maxHeight)
            this.$editor.css('max-height', this.opts.maxHeight);

        if (this.opts.toolbarExternal)
        {
            this.$box.css('top', this.boxcss.top);
            this.$toolbar.css({
                'width': this.toolcss.width,
                'top': this.toolcss.top,
                'position': this.toolcss.position
            });
        }

        if (!this.opts.iframe)
            this.$editor.css('height', height);
        else
            this.$frame.css('height', height);

        this.$editor.css('height', height);
        this.focus();
        this.observeStart();
    },
    toggleFullscreen: function()
    {
        if (!this.fullscreen)
        {
            this.enableFullScreen();
        }
        else
        {
            this.disableFullScreen();
        }
    },
    fullscreenIframe: function(html)
    {
        this.$editor = this.$frame.contents().find('body');
        this.$editor.attr({'contenteditable': true, 'dir': this.opts.direction});

        // set document & window
        if (this.$editor[0])
        {
            this.document = this.$editor[0].ownerDocument;
            this.window = this.document.defaultView || window;
        }

        // iframe css
        this.iframeAddCss();

        if (this.opts.fullpage)
            this.setFullpageOnInit(html);
        else
            this.set(html);

        if (this.opts.wym)
            this.$editor.addClass('redactor_editor_wym');
    },
    fullScreenResize: function()
    {
        if (!this.fullscreen)
            return false;

        var toolbarHeight = this.$toolbar.height();

        var pad = this.$editor.css('padding-top').replace('px', '');
        var height = $(window).height() - toolbarHeight;
        this.$box.width($(window).width() - 2).height(height + toolbarHeight);

        if (this.opts.toolbarExternal)
        {
            this.$toolbar.css({
                'top': '0px',
                'position': 'absolute',
                'width': '100%'
            });

            this.$box.css('top', toolbarHeight + 'px');
        }

        if (!this.opts.iframe)
            this.$editor.height(height - (pad * 2));
        else
        {
            setTimeout($.proxy(function()
            {
                this.$frame.height(height);

            }, this), 1);
        }

        this.$editor.height(height);
    }
};

if (!RedactorPlugins)
    var RedactorPlugins = {};

RedactorPlugins.fontcolor = {
    init: function()
    {
        var colors = [
            '#ffffff', '#000000', '#eeece1', '#1f497d', '#4f81bd', '#c0504d', '#9bbb59', '#8064a2', '#4bacc6', '#f79646', '#ffff00',
            '#f2f2f2', '#7f7f7f', '#ddd9c3', '#c6d9f0', '#dbe5f1', '#f2dcdb', '#ebf1dd', '#e5e0ec', '#dbeef3', '#fdeada', '#fff2ca',
            '#d8d8d8', '#595959', '#c4bd97', '#8db3e2', '#b8cce4', '#e5b9b7', '#d7e3bc', '#ccc1d9', '#b7dde8', '#fbd5b5', '#ffe694',
            '#bfbfbf', '#3f3f3f', '#938953', '#548dd4', '#95b3d7', '#d99694', '#c3d69b', '#b2a2c7', '#b7dde8', '#fac08f', '#f2c314',
            '#a5a5a5', '#262626', '#494429', '#17365d', '#366092', '#953734', '#76923c', '#5f497a', '#92cddc', '#e36c09', '#c09100',
            '#7f7f7f', '#0c0c0c', '#1d1b10', '#0f243e', '#244061', '#632423', '#4f6128', '#3f3151', '#31859b', '#974806', '#7f6000'
        ];

        var buttons = ['fontcolor', 'backcolor'];

        for (var i = 0; i < 2; i++)
        {
            var name = buttons[i];

            var $dropdown = $('<div class="redactor_dropdown redactor_dropdown_box_' + name + '" style="display: none; width: 243px;">');

            this.pickerBuild($dropdown, name, colors);
            $(this.$toolbar).append($dropdown);

            var btn = this.buttonAdd(name, this.opts.curLang[name], $.proxy(function(btnName, $button, btnObject, e)
            {
                this.dropdownShow(e, btnName);

            }, this));

            btn.data('dropdown', $dropdown);
        }
    },
    pickerBuild: function($dropdown, name, colors)
    {
        var rule = 'color';
        if (name === 'backcolor')
            rule = 'background-color';

        var _self = this;
        var onSwatch = function(e)
        {
            e.preventDefault();

            var $this = $(this);
            _self.pickerSet($this.data('rule'), $this.attr('rel'));

        }

        var len = colors.length;
        for (var z = 0; z < len; z++)
        {
            var color = colors[z];

            var $swatch = $('<a rel="' + color + '" data-rule="' + rule + '" href="#" style="float: left; font-size: 0; border: 2px solid #fff; padding: 0; margin: 0; width: 20px; height: 20px;"></a>');
            $swatch.css('background-color', color);
            $dropdown.append($swatch);
            $swatch.on('click', onSwatch);
        }

        var $elNone = $('<a href="#" style="display: block; clear: both; padding: 4px 0; font-size: 11px; line-height: 1;"></a>')
                .html(this.opts.curLang.none)
                .on('click', function(e)
                {
                    e.preventDefault();
                    _self.pickerSet(rule, false);
                });

        $dropdown.append($elNone);
    },
    pickerSet: function(rule, type)
    {
        this.bufferSet();

        this.$editor.focus();
        this.inlineRemoveStyle(rule);
        if (type !== false)
            this.inlineSetStyle(rule, type);
        if (this.opts.air)
            this.$air.fadeOut(100);
        this.sync();
    }
};
if (!RedactorPlugins)
    var RedactorPlugins = {};

RedactorPlugins.fontsize = {
    init: function()
    {
        var fonts = [10, 11, 12, 14, 16, 18, 20, 24, 28, 30];
        var that = this;
        var dropdown = {};

        $.each(fonts, function(i, s)
        {
            dropdown['s' + i] = {title: s + 'px', callback: function() {
                    that.setFontsize(s);
                }};
        });

        dropdown['remove'] = {title: 'Remove font size', callback: function() {
                that.resetFontsize();
            }};

        this.buttonAdd('fontsize', 'Change font size', false, dropdown);
    },
    setFontsize: function(size)
    {
        this.inlineSetStyle('font-size', size + 'px');
    },
    resetFontsize: function()
    {
        this.inlineRemoveStyle('font-size');
    }
};
if (!RedactorPlugins)
    var RedactorPlugins = {};

RedactorPlugins.fontfamily = {
    init: function()
    {
        var fonts = ['Arial', 'Helvetica', 'Georgia', 'Times New Roman', 'Monospace'];
        var that = this;
        var dropdown = {};

        $.each(fonts, function(i, s)
        {
            dropdown['s' + i] = {title: s, callback: function() {
                    that.setFontfamily(s);
                }};
        });

        dropdown['remove'] = {title: 'Remove font', callback: function() {
                that.resetFontfamily();
            }};

        this.buttonAdd('fontfamily', 'Change font family', false, dropdown);
    },
    setFontfamily: function(value)
    {
        this.inlineSetStyle('font-family', value);
    },
    resetFontfamily: function()
    {
        this.inlineRemoveStyle('font-family');
    }
};
if (!RedactorPlugins)
    var RedactorPlugins = {};

RedactorPlugins.textdirection = {
    init: function()
    {
        var that = this;
        var dropdown = {};

        dropdown['ltr'] = {title: 'Left to right', callback: function() {
                that.ltrTextDirection();
            }};
        dropdown['rtl'] = {title: 'Right to left', callback: function() {
                that.rtlTextDirection();
            }};

        this.buttonAdd('direction', 'Change direction', false, dropdown);
    },
    rtlTextDirection: function()
    {
        this.bufferSet();
        this.blockSetAttr('dir', 'rtl');
    },
    ltrTextDirection: function()
    {
        this.bufferSet();
        this.blockRemoveAttr('dir');
    }
};
jQuery(function($) {
    $('.redactor').each(function() {
        $(this).redactor({
            fixed: true,
            imageGetJson: $('#lista_upload').val(),
            imageUpload: $('#midia_upload').val(),
            fileUpload: $('#file_upload').val(),
            lang: 'pt_br',
            paragraphy: false,
            convertDivs: false,
            phpTags: true,
            tidyHtml: false,
            xhtml: true,
            plugins: ['fontfamily', 'fontsize', 'fontcolor', 'fullscreen'],
            imageUploadErrorCallback: function(json) {
                $('.msg_sistema').html('<div class="alert alert-danger"><a class="close" data-dismiss="alert" href="#">×</a>' + json.error + '</div>');
            }
        });
    });
});